<template>
	<view class="container flex_C">
		<FeNavbar title="" :isBorder="false" />
		<view class="top_wrap flexV_sb">
			<view class="title">消息中心</view>
			<view class="btn flexHV" @click="onRead">
				<text class="text">全部已读</text>
				<image class="icon" src="/static/common/del_icon.png" mode=""></image>
			</view>
		</view>
		<view class="flex_1">
			<z-paging ref="paging" refresher-only @onRefresh="onRefresh" height="100%" :fixed="false">
				<view class="item_wrap" v-for="(item,index) in list" :key="index">
					<view class="top_box flexV" @click="toSubList(item,index)">
						<image class="icon" :src="item.iconUrl" mode=""></image>
						<text class="title">{{item.category}}</text>
						<image class="r_icon" src="/static/mine/row-right.png" mode=""></image>
					</view>
					<view class="bottom_box" v-if="item.message">
						<view class="flexV_sb">
							<text class="mess_name van_ellipsis">{{item.message.title}}</text>
							<text class="time">{{timeFormat(item.message.createTime)}}</text>
						</view>
						<view class="content_wrap flexV">
							<view class="content van_ellipsis">{{item.message.summary}}</view>
							<view class="read" v-if="!item.message.isRead"></view>
						</view>
					</view>
				</view>
			</z-paging>
		</view>
	</view>
</template>

<script>
	import {
		messageApi,
		readApi
	} from '@/api/message.js'
	export default {
		data() {
			return {
				list: []
			}
		},
		onLoad() {},
		onShow() {
			uni.showLoading({
				mask: true
			})
			this.getList()
		},
		methods: {
			onRefresh() {
				Promise.all([this.getList()]).then(() => {
					this.$refs.paging.complete();
				})
			},
			async getList() {
				const res = await messageApi()
				uni.hideLoading()
				if (res.code == 200) {
					this.list = res.data.messageCategory;
				}
			},
			timeFormat(time) {
				const now = Date.parse(new Date())
				const diff = (now / 1000) - time
				if (diff < 60) {
					return `${diff}秒前`
				} else if (diff < (60 * 60)) {
					return `${Math.floor(diff/60)}分钟前`
				} else if (diff < (60 * 60 * 24)) {
					return `${Math.floor(diff/60/60)}小时前`
				}
				return `${Math.floor(diff/60/60/24)}天前`
			},
			toSubList(item, index) {
				uni.navigateTo({
					url: `/pages/common/notificationList?type=${item.categoryType}&title=${item.category}`
				})
			},
			async onRead() {
				uni.showLoading({
					mask: true
				})
				const res = await readApi({})
				uni.hideLoading()
				if (res.code == 200) {
					this.getList()
					uni.showToast({
						title: '已读完成',
						icon: 'success'
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		height: 100vh;

		&>.flex_1 {
			height: 0;
		}

		.top_wrap {
			padding: 32rpx;

			.title {
				font-weight: 600;
				font-size: 50rpx;
				color: #26282B;
			}

			.btn {
				width: 160rpx;
				height: 54rpx;
				background: #EEF1F6;
				border-radius: 200rpx 200rpx 200rpx 200rpx;

				.text {
					font-weight: 400;
					font-size: 24rpx;
					color: #A6ACB4;
				}

				.icon {
					width: 24rpx;
					height: 24rpx;
					margin-left: 8rpx;
				}
			}
		}

		.item_wrap {
			padding: 0 32rpx;
			padding-top: 36rpx;

			.top_box {
				margin-bottom: 12rpx;

				.icon {
					width: 60rpx;
					height: 60rpx;
					border-radius: 50%;
					overflow: hidden;
					background-color: #7B828A;
				}

				.title {
					font-weight: 600;
					font-size: 30rpx;
					color: #26282B;
					margin: 0 auto 0 24rpx;
				}

				.r_icon {
					width: 24rpx;
					height: 24rpx;
				}
			}

			.bottom_box {
				margin-left: 84rpx;
				padding: 16rpx 0;
				border-bottom: 1px solid #DBE0E6;

				.mess_name {
					font-weight: 600;
					font-size: 30rpx;
					color: #7B828A;
				}

				.time {
					font-weight: 400;
					font-size: 24rpx;
					color: #A6ACB4;
					margin-left: 24rpx;
				}

				.content_wrap {
					margin-top: 12rpx;
					overflow: hidden;
				}

				.content {
					flex: 1;
					font-weight: 400;
					font-size: 26rpx;
					color: #A6ACB4;
				}

				.read {
					width: 18rpx;
					height: 18rpx;
					background: #F04848;
					border-radius: 50%;
					margin-left: 24rpx;
				}
			}
		}
	}
</style>